<?php

class product_model extends FlexiGrid_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function getToken($q=""){
        $where=array("Delete"=>null);
        
        $query=$this->db
                ->select("ProductID,ProductName,Image,UnitPrice")
                ->like('ProductName', $q)
                ->get_where('dtb_product', $where); 
        return $query->result();
    }
    function getIn($list=array()){
        $where=array("Delete"=>null);
        
        $query=$this->db
                ->where_in('ProductID', $list)
                ->where($where)
                ->get('dtb_product',10,0); 
        return $query->result();
    }
    function get($ID=""){
        $where=array("ProductID"=>$ID);
        $query=$this->db->get_where('dtb_product', $where); 
        return $query->result();
    }
    function gets(){
        $query=$this->db->get_where('dtb_product'); 
        return $query->result();
    }
    function update($ID,$Params){
        $this->db->set('Update', "NOW()",false)
            ->where('ProductID', $ID)
            ->update('dtb_product', $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function insert($Params){
        $this->db->set('Insert', "NOW()",false)
            ->insert('dtb_product', $Params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($ID){
        $this->db->set('Delete', "NOW()",false);
        $this->db->where('ProductID', $ID);
        $this->db->update('dtb_product'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function restore($ID){
        $this->db->set('Delete', "NULL",false);
        $this->db->where('ProductID', $ID);
        $this->db->update('dtb_product'); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function FlexiGridData(){
        $configs["strQuery"]="
            SELECT SQL_CALC_FOUND_ROWS `dtb_product`.*
            FROM `dtb_product`
            ";
        $configs["strWhere"]="
            WHERE true
            ";
        if(isset($_SESSION["ADP"]["Filter_".CCTRL]["Filters"])){
            if($_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=="none"){
                $configs["strWhere"].=" AND false ";
            }elseif($_SESSION["ADP"]["Filter_".CCTRL]["FilterType"]=="me"){
                $strFilters="";
                foreach ($_SESSION["ADP"]["Filter_".CCTRL]["Filters"] as $f){
                    $f= mysql_escape_string($f);
                    $strFilters.="'$f',";
                }
                $strFilters.="'0-9a-zA-Z'";
                $configs["strWhere"].=" AND Categorys in ($strFilters) ";
            }
        }
        if( $_SESSION["ADP"][CCTRL]["display"]==0){
            $configs["strWhere"].=" AND `dtb_product`.`Delete` IS NULL";
        }elseif( $_SESSION["ADP"][CCTRL]["display"]==-1){
            $configs["strWhere"].=" AND `dtb_product`.`Delete` IS NOT NULL";
        }
        $configs["strOrderBy"]=" ORDER BY `Insert` DESC";
        $configs["fields"]=array(
            "ID"=>"ProductID",
            );
        $configs["filterfields"]=array(
            "ProductName","Supplier","ProductTitle","Categorys"
        );
        $this->init($configs);
        return $this->Binding();
    }
    
}
?>
